package com.neuedu.studentmanage.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.studentmanage.entity.Pager;
import com.neuedu.studentmanage.entity.Result;
import com.neuedu.studentmanage.entity.DormManager;
import com.neuedu.studentmanage.service.DormManagerService;

@Controller
public class DormManagerController {
	
	@Autowired
	private DormManagerService dormManagerService;
	
	@RequestMapping("/dormManager/index")
	public String doindex() {
		return "admin/dormManagerList";
	}
	
	@RequestMapping("admin/dormManagerList")
	@ResponseBody
	public Pager<DormManager> doList(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer rows) {
		Pager<DormManager> pager = new Pager<>(page, rows);
		
		List<DormManager> list = dormManagerService.findListByPager(pager);
		for (DormManager dormManager : list) {
			System.out.println(dormManager);
		}
		Integer total = dormManagerService.findTotalByPager(pager);

		pager.setRows(list);
		pager.setTotal(total);

		return pager;
	}
	
	@RequestMapping("dormManager/listAll")
	@ResponseBody
	public List<DormManager> doFindAll(){
		List<DormManager> dormManagerList = dormManagerService.findAll();
		return dormManagerList;
	}
	
	@RequestMapping("dormManager/add")
	@ResponseBody
	public Result doAdd(DormManager dormManager) {
		Result result = dormManagerService.add(dormManager);
		return result;
	}

	@RequestMapping("dormManager/edit")
	@ResponseBody
	public Result doEdit(DormManager dormManager) {
		Result result = dormManagerService.edit(dormManager);
		return result;
	}

	@RequestMapping("dormManager/delete")
	@ResponseBody
	public Result doDelete(Integer mgrId) {
		List<DormManager> list = new ArrayList<DormManager>();
		list.add(new DormManager(mgrId));

		Result result = dormManagerService.deleteByIds(list);
		return result;
	}

}
